﻿namespace AnyOffice.general.gzzd
{
    using log4net;
    using System;
    using System.Data;
    using System.IO;
    using System.Reflection;
    

    public class LiteUtility
    {

        private DataTable dtbInfo1;

        private DataTable dtbInfo2;

        private string diskPath = string.Empty;
      
        private static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        public LiteUtility()
        {
            GzzdInfo gzzdInfo = GzzdFactory.GetGzzdInfo();
            if (gzzdInfo != null) this.diskPath = gzzdInfo.DiskPath;
        }

        private void GetDirList(DirectoryInfo directory)
        {
            foreach (DirectoryInfo info in directory.GetDirectories())
            {
                string str = info.FullName.Replace(this.diskPath, "").Replace(@"\", "/");
                DataRow row = this.dtbInfo1.NewRow();
                row[1] = str;
                this.dtbInfo1.Rows.Add(row);
                this.GetDirList(info);
            }
        }

        private long GetDirSize(DirectoryInfo directory)
        {
            long num = 0;
            foreach (FileInfo info in directory.GetFiles())
            {
                num += info.Length;
            }
            foreach (DirectoryInfo info2 in directory.GetDirectories())
            {
                num += this.GetDirSize(info2);
            }
            return num;
        }

        public DataTable GetDtbDirectory()
        {
            this.dtbInfo1 = new DataTable("Directory");
            this.dtbInfo1.Columns.Add(new DataColumn("index", typeof(int)));
            this.dtbInfo1.Columns.Add(new DataColumn("path", typeof(string)));
            this.dtbInfo1.Columns[0].AutoIncrement = true;
            this.dtbInfo1.Columns[0].Unique = true;
            this.dtbInfo1.Rows.Add(this.dtbInfo1.NewRow());
            this.dtbInfo1.Rows[0]["path"] = "/";
            if (this.diskPath != string.Empty)
            {
                try
                {
                    if (this.diskPath != string.Empty) this.GetDirList(new DirectoryInfo(this.diskPath));
                }
                catch (Exception exception)
                {
                    if (log.IsErrorEnabled) log.Error("", exception);
                }
            }
            return this.dtbInfo1;
        }

        public DataTable GetDtbSearch(string relativePath, string pattern)
        {
            this.dtbInfo2 = new DataTable("Directory");
            this.dtbInfo2.Columns.Add(new DataColumn("index", typeof(int)));
            this.dtbInfo2.Columns.Add(new DataColumn("path", typeof(string)));
            this.dtbInfo2.Columns.Add(new DataColumn("name", typeof(string)));
            this.dtbInfo2.Columns.Add(new DataColumn("size", typeof(int)));
            this.dtbInfo2.Columns.Add(new DataColumn("creationTime", typeof(DateTime)));
            this.dtbInfo2.Columns.Add(new DataColumn("img", typeof(string)));
            this.dtbInfo2.Columns[0].AutoIncrement = true;
            this.dtbInfo2.Columns[0].Unique = true;
            if (this.diskPath != string.Empty)
            {
                string diskPath = this.diskPath;
                if (relativePath != "/") diskPath = diskPath + relativePath.Replace("../", "");
                diskPath = diskPath.Replace("/", @"\");
                try
                {
                    this.GetFileIcon(new DirectoryInfo(diskPath), pattern);
                }
                catch (Exception exception)
                {
                    if (log.IsErrorEnabled) log.Error("", exception);
                }
            }
            return this.dtbInfo2;
        }

        private void GetFileIcon(DirectoryInfo directory, string pattern)
        {
            string str = ".docx.xlsx.pptx.ace.app.arj.asp.av.gif.avi.bmp.cab.chm.com.css.dll.doc.down.exe.gif.htm.html.img.jpeg.jpg.js.m3u.mdb.mid.mov.mp3.mpeg.mpg.pdf.ppt.ra.ram.rar.rm.sql.swf.tar.txt.url.wav.wmv.xls.zip.";
            foreach (FileInfo info in directory.GetFiles(pattern))
            {
                DataRow row = this.dtbInfo2.NewRow();
                string str2 = info.FullName.Replace(this.diskPath, "").Replace(@"\", "/");
                row = this.dtbInfo2.NewRow();
                row[1] = str2;
                row[2] = info.Name;
                row[3] = info.Length;
                row[4] = info.LastWriteTime;
                if (str.IndexOf(info.Extension.ToLower() + ".") >= 0 && info.Extension != string.Empty)
                    row[5] = info.Extension.Replace(".", "") + ".gif";
                else
                    row[5] = "unknown.gif";
                this.dtbInfo2.Rows.Add(row);
            }
            foreach (DirectoryInfo info2 in directory.GetDirectories())
            {
                this.GetFileIcon(info2, pattern);
            }
        }

        public long SpecifyDirSize(string relativePath)
        {
            long dirSize = 0;
            if (this.diskPath != string.Empty)
            {
                try
                {
                    string diskPath = this.diskPath;
                    if (relativePath != "/") diskPath = diskPath + relativePath;
                    relativePath = relativePath.Replace("/", @"\");
                    dirSize = this.GetDirSize(new DirectoryInfo(diskPath));
                }
                catch (Exception exception)
                {
                    if (log.IsErrorEnabled) log.Error("", exception);
                }
            }
            return dirSize;
        }
    }
}

